package cn.lblbc.webbrowser.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import cn.lblbc.webbrowser.R
import java.io.File

class ImageGridAdapter(
    private val onImageClick: (File) -> Unit,
    private val onImageLongClick: (File) -> Unit = {},
    private val onSelectionChanged: (Int) -> Unit = {}
) : RecyclerView.Adapter<ImageGridAdapter.ImageViewHolder>() {

    private val imageFiles = mutableListOf<File>()
    private var selectedImages: MutableSet<File> = mutableSetOf()
    private var isSelectionMode: Boolean = false

    class ImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.imageView)
        val checkBox: CheckBox = itemView.findViewById(R.id.selectionCheckBox)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_collected_image, parent, false)
        return ImageViewHolder(view)
    }

    override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
        val imageFile = imageFiles[position]
        val isSelected = selectedImages.contains(imageFile)
        
        // 使用Glide加载图片
        Glide.with(holder.itemView.context)
            .load(imageFile)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.ic_image_placeholder)
            .error(R.drawable.ic_image_placeholder)
            .centerCrop()
            .into(holder.imageView)
        
        // 设置选择状态
        holder.checkBox.visibility = if (isSelectionMode) View.VISIBLE else View.GONE
        holder.checkBox.isChecked = isSelected
        
        // 设置选中状态的视觉效果
        holder.itemView.alpha = if (isSelectionMode && isSelected) 0.7f else 1.0f
        
        // 点击事件
        holder.itemView.setOnClickListener {
            if (isSelectionMode) {
                toggleSelection(imageFile)
            } else {
                onImageClick(imageFile)
            }
        }
        
        // 长按事件
        holder.itemView.setOnLongClickListener {
            if (!isSelectionMode) {
                enterSelectionMode()
                toggleSelection(imageFile)
                onImageLongClick(imageFile)
            }
            true
        }
        
        // 复选框点击事件
        holder.checkBox.setOnClickListener {
            toggleSelection(imageFile)
        }
    }

    override fun getItemCount(): Int = imageFiles.size
    
    fun updateImages(newImages: List<File>) {
        imageFiles.clear()
        imageFiles.addAll(newImages)
        selectedImages.clear()
        notifyDataSetChanged()
    }
    
    fun enterSelectionMode() {
        isSelectionMode = true
        selectedImages.clear()
        notifyDataSetChanged()
    }
    
    fun exitSelectionMode() {
        isSelectionMode = false
        selectedImages.clear()
        notifyDataSetChanged()
    }
    
    fun selectAll() {
        selectedImages.clear()
        selectedImages.addAll(imageFiles)
        notifyDataSetChanged()
        onSelectionChanged(selectedImages.size)
    }
    
    fun clearSelection() {
        selectedImages.clear()
        notifyDataSetChanged()
        onSelectionChanged(0)
    }
    
    fun getSelectedImages(): List<File> = selectedImages.toList()
    
    fun getSelectedCount(): Int = selectedImages.size
    
    fun isInSelectionMode(): Boolean = isSelectionMode
    
    private fun toggleSelection(imageFile: File) {
        if (selectedImages.contains(imageFile)) {
            selectedImages.remove(imageFile)
        } else {
            selectedImages.add(imageFile)
        }
        notifyDataSetChanged()
        onSelectionChanged(selectedImages.size)
    }
    
    fun areAllSelected(): Boolean {
        return selectedImages.size == imageFiles.size && imageFiles.isNotEmpty()
    }
}